import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;

public class DataBaseManager {
	private Connection db_connection;
	private java.sql.Statement stmt;
	private java.sql.ResultSet resultData;
	private java.sql.ResultSetMetaData resultMetaData;
	
	static public class Search_DBData {
		private int NumOfColumn;
		private ArrayList<Object[]> s_Data;

		public Search_DBData() {
			s_Data = new ArrayList<Object[]>();
			NumOfColumn = 0;
		}

		public void setNumOfColumn(int NumOfColumn) {
			this.NumOfColumn = NumOfColumn;
		}

		public void addElement(Object[] data) {
			s_Data.add(data);
		}
		
		public Object[] GetElement(int index) {
			return s_Data.get(index);
		}

		public void clearDatalist() {
			s_Data.clear();
		}

		public int getNumOfColumn() {
			return NumOfColumn;
		}

		public int getNumOfData() {
			return s_Data.size();
		}

	}

	public DataBaseManager() {
		try {
			Class.forName("com.mysql.jdbc.Driver");

			db_connection = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/test", "root", "apdlwl12");
			stmt = db_connection.createStatement();
			System.out.println("DB connected");
			
			
		} catch (ClassNotFoundException e) {
			System.out.println("DB driver doesn't exist");
		} catch (SQLException e) {
			System.out.println("DB Connection err");
		}
	}

	public void DB_disconnect() {
		try {
			db_connection.close();
		} catch (SQLException e) {
			System.out.println("DB Disconnecton err");
		}
	}

	private Search_DBData SelectQuery(String statement) {
		try {
			resultData = stmt.executeQuery(statement); //query
			resultMetaData = resultData.getMetaData(); //get metaData
			
			if(!resultData.next()) return null; // nothing found
			
			Search_DBData db_data = new Search_DBData(); // data structure create
			int num_of_Column = resultMetaData.getColumnCount(); //get num of DB data column
			db_data.setNumOfColumn(num_of_Column); //set num of column
			
			// Data search processing
			int type;
			
			resultData.beforeFirst();
			while(resultData.next()){
				Object[] data = new String[num_of_Column+1];
				for(int i=1; i<=num_of_Column; i++){
					type = resultMetaData.getColumnType(i);
					
					switch(type)
					{
					case Types.INTEGER:
						data[i] = resultData.getInt(i);
						break;
					case Types.FLOAT:
						data[i] = resultData.getFloat(i);
						break;
					case Types.DOUBLE:
						data[i] = resultData.getDouble(i);
						break;
					case Types.CHAR:
					case Types.VARCHAR:
					case Types.LONGVARCHAR:
						data[i] = resultData.getString(i);
						break;
					case Types.DATE:
						data[i] = resultData.getDate(i);
						
						break;
					case Types.TIME:
						data[i] = resultData.getTime(i);
						break;
					default:
						System.out.println("abnormal data type");
					}
				}
				db_data.addElement(data);
			}
			
			return db_data;
			
		} catch (Exception e) {
			System.out.println("Query err");
		}

		return null;
	}
	
	private int UpdateQurey(String statement)
	{
		int rowcount;
		 try {
			rowcount = stmt.executeUpdate(statement);
			return rowcount;
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
		
	}
	
	public Object Query(String statement)
	{
		String temp_Upper_String = statement.toUpperCase();
		if(temp_Upper_String.contains("SELECT")) return SelectQuery(statement);
		else if(temp_Upper_String.contains("UPDATE") ||
				temp_Upper_String.contains("INSERT") || temp_Upper_String.contains("DELETE"))
			return UpdateQurey(statement);
			
		return null;
	}
	
}
